# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from openpyxl import Workbook
import os

class MaoyanPipeline(object):
    # def __init__(self):
    #     self.wb = Workbook()
    #     self.ws = self.wb.active
    #     self.ws.append(['电影名字', '上映时间', '电影评分'])
    def process_item(self, item, spider):
        base_dir = os.getcwd()
        fiename = base_dir + '/maoyan.txt'
        with open(fiename, 'a') as f:
            f.write(item['filmnames'][0:100] + '\n')
            f.write(item['filmdate'][0] + '\n')
            f.write(item['filmscore'] + '\n\n')

        # line = [item['filmnames'][0:100], item['filmdate'][0], item['filmscore']]
        # self.ws.append(line)
        # self.wb.save('maoyantop20.xlsx')

        print('电影名为：{}'.format(item['filmnames']))
        print('上映时间为：{}'.format(item['filmdate']))
        print('电影评分为：{}'.format(item['filmscore']))


        return item

